package com.wl.eb.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.eb.module.EbProduct;
import com.wl.eb.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Resource
    private ProductService productService;
    @RequestMapping("/detail")
    public String detail(Model model,int productId) throws Exception{
        //调用service根据ProductId查询该商品的详情
        EbProduct productdetail = productService.detail(productId);
        if(productdetail==null){
        throw new RuntimeException("查询的商品不存在！！");}
        //将详情数据绑定到MOdel中
        model.addAttribute("detail",productdetail);
        //返回view
        return  "product-view";//product-view.html
    }

    @RequestMapping("/list")
    public String productlistCategory(Model model,
                                      Integer categoryId,
                                      @RequestParam(value = "page",defaultValue = "1") int page){

        System.out.println("===进入productlistCategory。。。。分类id为："+categoryId+",页数是："+page);
        //调用service根据CategoryId查询该分页数据
        Page productPage = productService.list(page,categoryId);
        //将详情数据绑定到MOdel中
        model.addAttribute("productpage",productPage);
        //返回view
        return "product-list";//product-list.html
    }


    @GetMapping("/add/car")
    public  String addCar(int id, HttpServletRequest request){
        System.out.println("进入ProductController的addCar方法，id："+id);
        //1.调用service将商品添加到购物车
        productService.addCar(id,request);
        //2.绑定数据到model
        //3.返回view
        return "shopping";
    }

    @GetMapping("/shop/result")
    public  String shopResult( HttpServletRequest request){
        request.getSession().removeAttribute("car");
        return "shopping-result";
    }

    @GetMapping("/list1")
    public String page1(Model model,@RequestParam(value = "page",defaultValue = "1") int page){
        System.out.println("======进入IndexController的index方法=====page:"+page);
        //1.查询首页需要展示的数据
        Map<String,Object> result=productService.spage(page);
        System.out.println("------result:------"+result);
        //2.将数据放到model
        model.addAttribute("productpage",(Page)result.get("ebProductPage"));
        //3，返回view———视图   html
        return "product-list";//templates/index.html
    }


}


